package com.ce.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ce.core.AjaxResult;
import com.ce.entity.SalesList;
import com.ce.entity.ShoppingList;
import com.ce.entity.Storage;
import com.ce.service.SalesListService;
import com.ce.service.ShoppingListService;
import com.ce.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售记录表 前端控制器
 * </p>
 *
 * @author jyt
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/sales-list")
public class SalesListController {
    @Autowired
    SalesListService service;

    @Autowired
    ShoppingListService shoppingListService;

    @Autowired
    StorageService storageService;

    /**
     * 查询销售信息记录
     */
    @PostMapping("/getSalesList")
    public List<SalesList> testSelect(SalesList salesList) {
        List<SalesList> salesLists = service.getList(salesList);
        return salesLists;
    }

    /**
     *
     * 查询销售金额
     */
    @GetMapping("/getSalesListPrice")
    public List<SalesList> selectPrice() {
        List<SalesList> salesLists = service.getListForPrice();
        return salesLists;
    }

    /**
     *
     * 销售金额查询（带查询参数）
     */
    @PostMapping("/getSalesListsPrice")
    public List<SalesList> selectSalesPrice(SalesList salesList) {
        List<SalesList> salesLists = service.getSalesList(salesList);
        return salesLists;
    }

    /**
     *
     * 预测销售数
     */
    @GetMapping("/predictCount")
    public AjaxResult predictCount(){
        int avgCount = service.selectCount() / service.selectSaleMonthCount();
        System.out.println(avgCount);
        return AjaxResult.success(avgCount);
    }
    /**
     *
     * 销售笔数
     */
    @GetMapping("/count")
    public AjaxResult count(){
        int count = service.selectCount();
        System.out.println("销售笔数:"+count);
        return AjaxResult.success(count);
    }

    /**
     *
     * 预测销售金额
     */
    @GetMapping("/predictPrice")
    public AjaxResult predictPrice(){
        double avgPrice = service.selectAllPrice() / service.selectSaleMonthCount();
        BigDecimal two = new BigDecimal(avgPrice);
        avgPrice = two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("平均销售金额："+avgPrice);
        return AjaxResult.success(avgPrice);
    }

    /**
     * 查询销售金额统计 y坐标 ----图表
     */
    @GetMapping("/getSalesListSumPrice/chart")
    public List<Object> selectPriceForChart() {
        List<SalesList> salesLists = service.getListForPrice();
        //定义List集合转化为纯数组
        List<Object> priceList = new ArrayList<>();
        for (int i = 0; i < salesLists.size(); i++) {
            //获取年龄
            priceList.add(salesLists.get(i).getSaleSumPrice());
            System.out.println("salesLists.get(i).getSaleSum():" + salesLists.get(i).getSaleSumPrice());
        }
        return priceList;
    }

    /**
     * 查询盈利金额统计 y坐标 ----图表
     */
    @GetMapping("/getSalesListGainSumPrice/chart")
    public List<Object> selectGainPriceForChart() {
        List<SalesList> salesLists = service.getListForPrice();
        List<ShoppingList> shoppingLists = shoppingListService.getListForPrice();
        for (int i = 0; i < shoppingLists.size(); i++) {
            System.out.println("shoppingLists:"+shoppingLists.get(i).getBuyPrice());
        }
        for (int i = 0; i < salesLists.size(); i++) {
            System.out.println("salesLists:"+salesLists.get(i).getSaleSumPrice());
        }
        //定义List集合转化为纯数组
        List<Object> priceList = new ArrayList<>();
         // TODO:增加判断哪个小
        for (int i = 0; i < (shoppingLists.size()>salesLists.size()?salesLists.size():shoppingLists.size()); i++) {
            System.out.println(salesLists.get(i).getProductNo()+" : "+shoppingLists.get(i).getProductNo());

            // 根据商品编号 计算盈利额
           /* if (salesLists.get(i).getProductNo().equals(shoppingLists.get(i).getProductNo())){
                priceList.add(salesLists.get(i).getSaleSumPrice() - shoppingLists.get(i).getBuyPrice());
            }*/
            for (int j = 0; j < (shoppingLists.size()<salesLists.size()?salesLists.size():shoppingLists.size()); j++) {
                if (salesLists.get(i).getProductNo().equals(shoppingLists.get(j).getProductNo())){
                    priceList.add(salesLists.get(i).getSaleSumPrice() - shoppingLists.get(j).getBuyPrice());
                }
            }

        }
        return priceList;
    }


    /**
     * 查询销售商品名称统计 x坐标 ----图表
     */
    @GetMapping("/getSalesListProductName/chart")
    public List<Object> selectNameForChart() {
        List<SalesList> salesLists = service.getListForPrice();
        //定义List集合转化为纯数组
        List<Object> nameList = new ArrayList<>();
        for (int i = 0; i < salesLists.size(); i++) {
            //获取年龄
            nameList.add(salesLists.get(i).getProductName());
            System.out.println("salesLists.get(i).getProductName():" + salesLists.get(i).getProductName());
        }
        return nameList;
    }

    /**
     * 添加销售记录
     */
    @PostMapping("/addSalesList")
    public AjaxResult insertSalesList(HttpServletRequest request) throws ParseException {
        SalesList salesList = new SalesList();
        salesList.setProductNo(request.getParameter("productNo"));
        salesList.setCustomerNo(request.getParameter("customerNo"));
        salesList.setSaleDate(request.getParameter("saleDate"));
        salesList.setSaleSumPrice(Double.parseDouble(request.getParameter("saleSumPrice")));
        salesList.setSaleActualPrice(Double.parseDouble(request.getParameter("saleActualPrice")));
        salesList.setSaleSum(Integer.parseInt(request.getParameter("saleSum")));
        salesList.setShopNo(request.getParameter("shopNo"));
        salesList.setWarehouseNo(request.getParameter("warehouseNo"));
        Storage storage = new Storage();
        storage.setWarehouseNo(request.getParameter("warehouseNo"));
        storage.setProductNo(request.getParameter("productNo"));
        storage.setStorageSum(Integer.parseInt(request.getParameter("saleSum")));
        System.out.println("warehouseNO:" + request.getParameter("warehouseNo"));
        // TODO：如果没有则提示无库存、有则更新
        if (ObjectUtils.isNotEmpty(storageService.selectByProductNoAndWarehouseNo(storage))) {
            Storage storage1 = storageService.selectByProductNoAndWarehouseNo(storage);
            // 判断库存数是否大于所输入的销售数量，如果大于则添加成功，并减少相应库存
            int saleSum = Integer.parseInt(request.getParameter("saleSum"));
            if(saleSum>storage1.getStorageSum()){
                return AjaxResult.error(500,"库存不足，无法添加销售单");
            }else {
                storageService.reduceStorageList(storage);
            }

        } else {
            System.out.println("无库存");
            return AjaxResult.error(500,"无库存,请选择有库存的仓库");
        }
        service.addSalesList(salesList);
        return AjaxResult.success("添加成功");
    }
}
